<template>
  <div class="knowledge-file-list">
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="sysAttachment" label="缩略图" width="100">
        <template slot-scope="scope">
          <div class="img-box">
            <img class="thumbnail" :src="scope.row | fileThumbnail" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="资源名称"> </el-table-column>
      <el-table-column prop="size" label="资源大小">
        <template slot-scope="scope">
          {{ scope.row.size | fileSizeFilter }}
        </template>
      </el-table-column>
      <el-table-column prop="type" label="资源类型">
          <template slot-scope="scope">
          <div >
            {{scope.row.sysAttachment.attrSuffixStr}}
          </div>
        
        </template>
     </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="success" size="mini" @click="handleLook(row)">
              预览
            </el-button>
            <el-button type="primary" size="mini" @click="handleDownload(row)">
              下载
            </el-button>
            <!-- <el-button
              v-if="row.type == 'jpg' || row.type == 'png'"
              type="primary"
              size="mini"
              @click="setCoverImg(row)"
              :disabled="row.fileId == $store.getters.knowledgeFiledId"
            >
              设为封面图
            </el-button> -->
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
    <Preview />
  </div>
</template>
<script>
import {
  sysCourseFilesQuery,
  sysCourseFilesDelete,
} from "@/api/admin/courseLibrary/sysCourseFiles.js";
import download from '@/mixins/download'
import { SetCoverImg } from "../../../courseLibrary/mixins/SetCoverImg";
import Bus from "@/utils/bus.js";
export default {
  mixins: [SetCoverImg,download],
  components: {
    Preview: () => import("@/components/resource/preview.vue"),
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        courseId: this.$store.getters.courseId,
        type:'1'
      },
      tableData: [],
      pageObj: {},
    };
  },
  mounted() {
    this.getList();
    Bus.$on("refreshKnowledgeFilesList", (v) => {
     
      if (v) {
        Object.assign(this.listQuery, v);
      }
      this.getList();
    });
    Bus.$on("refActiveNameFilesList", (v) => {
        
        this.listQuery.type=v;
        this.getList();
    });
  },
  beforeDestroy() {
    Bus.$off("refreshKnowledgeFilesList");
    Bus.$off("refActiveNameFilesList");
    
  },
  methods: {
    getList() {
      sysCourseFilesQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data;
        this.pageObj = res.data;
      });
    },
    //预览
    handleLook(row) {
      Bus.$emit("showPreview", row);
    },
    handleDownload(row){
      this.downloadCall(row.fileId, row.name)
    },
    //删除确认
    handleDelete(row) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delete(row);
        })
        .catch(() => {});
    },
    //删除
    delete(row) {
      sysCourseFilesDelete(row.id).then((res) => {
        this.getList();
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
  },
};
</script>
<style scoped>
.knowledge-file-list{
  padding-bottom: 20px;
}
.img-box {
  padding: 2px;
  width: auto;
  height: 40px;
  display: inline-block;
}
.thumbnail {
  width: auto;
  height: 100%;
  max-width: 60px;
}
</style>